
// src/api/domains.ts
import { apiClient } from './client';
import { Domain, DomainStats } from './types';

export const domainsApi = {
  // 获取虚拟机列表
  list: async (connectionId?: string) => {
    const url = connectionId 
      ? `/connections/${connectionId}/domains`
      : '/domains';
    const { data } = await apiClient.get<Domain[]>(url);
    return data;
  },

  // 获取单个虚拟机
  get: async (name: string, connectionId?: string) => {
    const url = connectionId
      ? `/connections/${connectionId}/domains/${name}`
      : `/domains/${name}`;
    const { data } = await apiClient.get<Domain>(url);
    return data;
  },

  // 创建虚拟机
  create: async (domainConfig: any, connectionId?: string) => {
    const url = connectionId
      ? `/connections/${connectionId}/domains`
      : '/domains';
    const { data } = await apiClient.post(url, domainConfig);
    return data;
  },

  // 删除虚拟机
  delete: async (name: string, connectionId?: string) => {
    const url = connectionId
      ? `/connections/${connectionId}/domains/${name}`
      : `/domains/${name}`;
    await apiClient.delete(url);
  },

  // 控制操作
  start: async (name: string, connectionId?: string) => {
    const url = connectionId
      ? `/connections/${connectionId}/domains/${name}/start`
      : `/domains/${name}/start`;
    await apiClient.post(url);
  },

  shutdown: async (name: string, connectionId?: string) => {
    const url = connectionId
      ? `/connections/${connectionId}/domains/${name}/shutdown`
      : `/domains/${name}/shutdown`;
    await apiClient.post(url);
  },

  forceStop: async (name: string, connectionId?: string) => {
    const url = connectionId
      ? `/connections/${connectionId}/domains/${name}/force-stop`
      : `/domains/${name}/force-stop`;
    await apiClient.post(url);
  },

  reboot: async (name: string, connectionId?: string) => {
    const url = connectionId
      ? `/connections/${connectionId}/domains/${name}/reboot`
      : `/domains/${name}/reboot`;
    await apiClient.post(url);
  },

  suspend: async (name: string, connectionId?: string) => {
    const url = connectionId
      ? `/connections/${connectionId}/domains/${name}/suspend`
      : `/domains/${name}/suspend`;
    await apiClient.post(url);
  },

  resume: async (name: string, connectionId?: string) => {
    const url = connectionId
      ? `/connections/${connectionId}/domains/${name}/resume`
      : `/domains/${name}/resume`;
    await apiClient.post(url);
  },

  // 配置修改
  setVCPUs: async (name: string, vcpus: number, connectionId?: string) => {
    const url = connectionId
      ? `/connections/${connectionId}/domains/${name}/vcpus`
      : `/domains/${name}/vcpus`;
    await apiClient.put(url, { vcpus });
  },

  setMemory: async (name: string, memory: number, connectionId?: string) => {
    const url = connectionId
      ? `/connections/${connectionId}/domains/${name}/memory`
      : `/domains/${name}/memory`;
    await apiClient.put(url, { memory });
  },

  // 统计信息
  getStats: async (name: string, connectionId?: string) => {
    const url = connectionId
      ? `/connections/${connectionId}/domains/${name}/stats`
      : `/domains/${name}/stats`;
    const { data } = await apiClient.get<DomainStats>(url);
    return data;
  },

  getStatus: async (name: string, connectionId?: string) => {
    const url = connectionId
      ? `/connections/${connectionId}/domains/${name}/status`
      : `/domains/${name}/status`;
    const { data } = await apiClient.get(url);
    return data;
  },
};